package org.kojo.tdrm.utils;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AppTokenUtils {
	private static Logger log = Logger.getLogger(AppTokenUtils.class.getName());
	private static AppTokenUtils instance = null;
	private String token = null;
	
	private AppTokenUtils(){
		setUpToken();
	}
	
	public static AppTokenUtils getInstance() {
		if (instance == null) {
			instance = new AppTokenUtils();
		}
		return instance;
	}
	private void setUpToken(){
		
		String tokenUrl = "https://graph.facebook.com/oauth/access_token?client_id="+App.APPID+"&client_secret="+App.APPSECRET+"&grant_type=client_credentials";
		HttpUtils u = new HttpUtils();
		String rawResp="";
		try {
			rawResp = u.getUrlContent(tokenUrl);
		} catch (IOException e) {
			log.log(Level.WARNING, "[app] Setup App Token Fail.", e);
		}
		String raw = u.getQueryMap(rawResp).get("access_token");
		token = raw.trim();
		log.config("[app] Setup App token = "+token);
		
	}
	
	public String getToken(){
		return token;
	}
}
